package cn.yerly.sakura.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.yerly.sakura.domain.TradeStateChangelog;
import cn.yerly.sakura.repository.TradeStateChangelogRepository;
import cn.yerly.sakura.repository.search.TradeStateChangelogSearchRepository;
import cn.yerly.sakura.web.rest.util.PaginationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.inject.Inject;
import javax.validation.Valid;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing TradeStateChangelog.
 */
@RestController
@RequestMapping("/api")
public class TradeStateChangelogResource {

    private final Logger log = LoggerFactory.getLogger(TradeStateChangelogResource.class);

    @Inject
    private TradeStateChangelogRepository tradeStateChangelogRepository;

    @Inject
    private TradeStateChangelogSearchRepository tradeStateChangelogSearchRepository;

    /**
     * POST  /tradeStateChangelogs -> Create a new tradeStateChangelog.
     */
    @RequestMapping(value = "/tradeStateChangelogs",
            method = RequestMethod.POST,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<Void> create(@Valid @RequestBody TradeStateChangelog tradeStateChangelog) throws URISyntaxException {
        log.debug("REST request to save TradeStateChangelog : {}", tradeStateChangelog);
        if (tradeStateChangelog.getId() != null) {
            return ResponseEntity.badRequest().header("Failure", "A new tradeStateChangelog cannot already have an ID").build();
        }
        tradeStateChangelogRepository.save(tradeStateChangelog);
        tradeStateChangelogSearchRepository.save(tradeStateChangelog);
        return ResponseEntity.created(new URI("/api/tradeStateChangelogs/" + tradeStateChangelog.getId())).build();
    }

    /**
     * PUT  /tradeStateChangelogs -> Updates an existing tradeStateChangelog.
     */
    @RequestMapping(value = "/tradeStateChangelogs",
        method = RequestMethod.PUT,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<Void> update(@Valid @RequestBody TradeStateChangelog tradeStateChangelog) throws URISyntaxException {
        log.debug("REST request to update TradeStateChangelog : {}", tradeStateChangelog);
        if (tradeStateChangelog.getId() == null) {
            return create(tradeStateChangelog);
        }
        tradeStateChangelogRepository.save(tradeStateChangelog);
        tradeStateChangelogSearchRepository.save(tradeStateChangelog);
        return ResponseEntity.ok().build();
    }

    /**
     * GET  /tradeStateChangelogs -> get all the tradeStateChangelogs.
     */
    @RequestMapping(value = "/tradeStateChangelogs",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<List<TradeStateChangelog>> getAll(@RequestParam(value = "page" , required = false) Integer offset,
                                  @RequestParam(value = "per_page", required = false) Integer limit)
        throws URISyntaxException {
        Page<TradeStateChangelog> page = tradeStateChangelogRepository.findAll(PaginationUtil.generatePageRequest(offset, limit));
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/tradeStateChangelogs", offset, limit);
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /tradeStateChangelogs/:id -> get the "id" tradeStateChangelog.
     */
    @RequestMapping(value = "/tradeStateChangelogs/{id}",
            method = RequestMethod.GET,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public ResponseEntity<TradeStateChangelog> get(@PathVariable Long id) {
        log.debug("REST request to get TradeStateChangelog : {}", id);
        return Optional.ofNullable(tradeStateChangelogRepository.findOne(id))
            .map(tradeStateChangelog -> new ResponseEntity<>(
                tradeStateChangelog,
                HttpStatus.OK))
            .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * DELETE  /tradeStateChangelogs/:id -> delete the "id" tradeStateChangelog.
     */
    @RequestMapping(value = "/tradeStateChangelogs/{id}",
            method = RequestMethod.DELETE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public void delete(@PathVariable Long id) {
        log.debug("REST request to delete TradeStateChangelog : {}", id);
        tradeStateChangelogRepository.delete(id);
        tradeStateChangelogSearchRepository.delete(id);
    }

    /**
     * SEARCH  /_search/tradeStateChangelogs/:query -> search for the tradeStateChangelog corresponding
     * to the query.
     */
    @RequestMapping(value = "/_search/tradeStateChangelogs/{query}",
        method = RequestMethod.GET,
        produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public List<TradeStateChangelog> search(@PathVariable String query) {
        return StreamSupport
            .stream(tradeStateChangelogSearchRepository.search(queryString(query)).spliterator(), false)
            .collect(Collectors.toList());
    }
}
